package cn.fl.mfr.web.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mfr.web.service.IMfrInfoModDtlService;
import cn.fl.mfr.web.transform.MfrInfoModDtlVOTransform;
import cn.fl.mfr.web.vo.MfrInfoModDtlVO;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.framework.enums.CommonOpTypCdEnum;
import fl.spl.facade.mfr.api.IMfrInfoModDtlApiService;
import fl.spl.facade.mfr.api.IMfrRlPsnApiService;
import fl.spl.facade.mfr.condition.MfrRlPsnQC;
import fl.spl.facade.mfr.constant.MfrInfoConstant;
import fl.spl.facade.mfr.dto.MfrInfoModDtlDTO;
import fl.spl.facade.mfr.dto.MfrRlPsnDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 厂商信息变更记录 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class MfrInfoModDtlServiceImpl implements IMfrInfoModDtlService {

	/**
	 * 自定义 API 自行注入需要的API服务
	 */
	@Autowired
	private IMfrInfoModDtlApiService mfrInfoModDtlApiService;

	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService ;

	@Autowired
	private IMfrRlPsnApiService mfrRlPsnApiService ;

	@Override
	public MfrInfoModDtlVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("主键参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_PROD_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "获取对象主键不能为空!");
		}
		//TODO 业务逻辑
		MfrInfoModDtlDTO dto = this.mfrInfoModDtlApiService.selectMfrInfoModDtlDTOById(id,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"","");
		return MfrInfoModDtlVOTransform.toVO(dto);
	}

	@Override
	public Long add(MfrInfoModDtlVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("新增对象不存在").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_PROD_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001,"新增对象不存在!");
		};
		return this.mfrInfoModDtlApiService.addMfrInfoModDtlDTO(MfrInfoModDtlVOTransform.toDTO(entity),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"","");
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,"删除对象主键不能为空!");
		};
		//return this.mfrInfoModDtlApiService.removeMfrInfoModDtlDTOById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,"删除对象主键不能为空!");
		};
		return false;
    }

	@Override
	public boolean modify(MfrInfoModDtlVO entity) throws BaseException{
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,"修改对象不能为空!");
		};
		DataResultDTO resultDTO = this.mfrInfoModDtlApiService.modifyMfrInfoModDtlDTO(MfrInfoModDtlVOTransform.toDTO(entity),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"","");
		if(resultDTO.getSuccess() == BaseConstant.IS_YESNO_YES){
			return true;
		}
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Map<String,String> payTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"MFR0001");
		Map<String,String> rlTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"SPL0004");
		page = this.mfrInfoModDtlApiService.searchMfrInfoModDtlListPage(page,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"","");
		List<MfrInfoModDtlDTO> mfrInfoModDtlDTOS = (List<MfrInfoModDtlDTO>) page.getResultList();
		if(CollectionUtils.isNotEmpty(mfrInfoModDtlDTOS)){
			List<MfrInfoModDtlVO> mfrInfoModDtlVOS = MfrInfoModDtlVOTransform.toVOList(mfrInfoModDtlDTOS);
			Map<Long,MfrRlPsnDTO> rlPsnMap = new HashMap<>();
			List<Long> ids = new ArrayList<>();
			for (MfrInfoModDtlDTO mfrInfoModDtlDTO : mfrInfoModDtlDTOS ) {
				if(mfrInfoModDtlDTO.getTabNm().equals(MfrInfoConstant.MFR_RL_PSN_FLOW_S_H_MFR_TAB)){
					ids.add(mfrInfoModDtlDTO.getModBusId());
				}
			}
			MfrRlPsnQC mfrRlPsnQC = new MfrRlPsnQC();
			mfrRlPsnQC.setIdList(ids);
			List<MfrRlPsnDTO> mfrRlPsnDTOS = this.mfrRlPsnApiService.searchMfrRlPsnDTOList(mfrRlPsnQC);
			for (MfrRlPsnDTO mfrRlPsnDTO : mfrRlPsnDTOS ) {
				rlPsnMap.put(mfrRlPsnDTO.getId(),mfrRlPsnDTO);
			}
			for (MfrInfoModDtlVO mfrInfoModDtlVO : mfrInfoModDtlVOS ) {
				if(mfrInfoModDtlVO.getModTypCd().equals(CommonOpTypCdEnum.MDF.getOpTypCd())){
					mfrInfoModDtlVO.setModTypCd(CommonOpTypCdEnum.MDF.getOpTypCdNm());
					if(mfrInfoModDtlVO.getTabNm().equals(MfrInfoConstant.MFR_ORG_R_FLOW_S_H_MFR_TAB)){
						mfrInfoModDtlVO.setModConBef(payTypCdMap.get(mfrInfoModDtlVO.getModConBef()));
						mfrInfoModDtlVO.setModConAft(payTypCdMap.get(mfrInfoModDtlVO.getModConAft()));
					}
				}else if(mfrInfoModDtlVO.getModTypCd().equals(CommonOpTypCdEnum.ADD.getOpTypCd())){
					mfrInfoModDtlVO.setModTypCd(CommonOpTypCdEnum.ADD.getOpTypCdNm());
					mfrInfoModDtlVO.setModConAft(rlPsnMap.get(mfrInfoModDtlVO.getModBusId()).getRlNm());
				}else if(mfrInfoModDtlVO.getModTypCd().equals(CommonOpTypCdEnum.DEL.getOpTypCd())){
					mfrInfoModDtlVO.setModTypCd(CommonOpTypCdEnum.DEL.getOpTypCdNm());
					mfrInfoModDtlVO.setModConBef(rlPsnMap.get(mfrInfoModDtlVO.getModBusId()).getRlNm());
				}
				StringBuffer rlRem = new StringBuffer();
				if(mfrInfoModDtlVO.getTabNm().equals(MfrInfoConstant.MFR_RL_PSN_FLOW_S_H_MFR_TAB)){
					if(mfrInfoModDtlVO.getModAttrFiled().equals("id")){
						rlRem.append(mfrInfoModDtlVO.getModAttrCon());
						rlRem.append(rlTypCdMap.get(rlPsnMap.get(mfrInfoModDtlVO.getModBusId()).getRlTypCd()));
						if(mfrInfoModDtlVO.getModTypCd().equals(CommonOpTypCdEnum.ADD.getOpTypCd())){
							rlRem.append(mfrInfoModDtlVO.getModConAft());
						}else if(mfrInfoModDtlVO.getModTypCd().equals(CommonOpTypCdEnum.DEL.getOpTypCd())){
							rlRem.append(mfrInfoModDtlVO.getModConBef());
						}
					}else{
						rlRem.append("联系人");
						rlRem.append(rlTypCdMap.get(rlPsnMap.get(mfrInfoModDtlVO.getModBusId()).getRlTypCd()));
						rlRem.append(mfrInfoModDtlVO.getModConBef());
						rlRem.append(mfrInfoModDtlVO.getModAttrCon());
					}
				}else{
					rlRem.append(mfrInfoModDtlVO.getModAttrCon());
				}
				rlRem.append(mfrInfoModDtlVO.getModTypCd());
				mfrInfoModDtlVO.setRlRem(rlRem.toString());
			}
			page.setResultList(mfrInfoModDtlVOS);
		}
		return page;
	}
}
